<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Question extends Model
{
    // 创建问题
    public function add()
    {
        // 检查用户是否登录
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }

        // 检查是否存在标题
        if (!rq('title')) {
            return ['status' => 0, 'msg' => 'required title'];
        }

        $this->title = rq('title');
        $this->user_id = session('user_id');
        // 如果存在描述就添加描述
        if (rq('desc')) {
            $this->desc = rq('desc');
        }
        // 保存
        return $this->save() ?
            ['status' => 1, 'id' => $this->id] :
            ['status' => 0, 'msg' => 'db insert failed'];
    }

    public function change()
    {
        // 检查用户是否登录(如果没有登录)
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }
        // 检查参数中是否有 id
        if (!rq('id')) {
            return ['status' => 0, 'msg' => 'id is required'];
        }
        // 获取指定Id 的 model (返回主键所对应的那一行)
        $question = $this->find(rq('id'));
        // 判断问题是否存在
        if (!$question) {
            return ['status' => 0, 'msg' => 'question not exists'];
        }
        if ($question->user_id != session('user_id')) {
            // 返回没有权限
            return ['status' => 0, 'msg' => 'permission denied'];
        }

        if (rq('title')) {
            $question->title = rq('title');
        }

        if (rq('desc')) {
            $question->desc = rq('desc');
        }
        // 保存数据
        return $question->save() ?
            ['status' => 1] :
            ['status' => 0, 'msg' => 'db update failed'];
    }

    // 查看问题api
    public function read()
    {
        // 请求参数中是否有 id, 如果有直接返回 id 所在的行
        if (rq('id')) {
            return ['status' => 1, 'data' => $this->find(rq('id'))];
        }
        // limit 条件
//        $limit = rq('limit') ? : 15;
        // skip 条件， 用于分页
//        $skip = (rq('page') ? rq('page') -1 : 0)  * $limit;
        list($limit, $skip) = pageinate(rq('page'), rq('limit'));

        // 构建 query 并返回 collection 数据
        $r = $this
            ->orderBy('created_at')
            ->limit($limit)
            ->skip($skip)
            ->get(['id', 'title', 'desc', 'user_id', 'created_at', 'updated_at'])
            ->keyBy('id');
        // collection
        return ['status' => 1, 'data' => $r];
    }

    // 删除问题 api
    public function remove()
    {
        // 检查用户是否登录
        if (!user_ins()->is_logged_in()) {
            return ['status' => 0, 'msg' => 'login required'];
        }
        // 检查传参中是否有 id
        if (!rq('id')) {
            return ['status' => 0, 'msg' => 'id is required'];
        }

        // 获取传参 id 所对应的 model
        $question = $this->find(rq('id'));
        if (!$question) return ['stauts' => 0, 'question not exists'];

        // 检查当前用户是否为问题的所有者
        if (session('user_id') != $question->user_id) {
            return ['status' => 0, 'permission denied'];
        }

        return $question->delete() ?
            ['status' => 1] :
            ['status' => 0, 'msg' => 'db delete failed'];
    }






}
